﻿using Craftsman.Core.Domain.Repositories;
using Craftsman.xCoronavirus.Domain.Contract;
using Craftsman.xCoronavirus.Domain.Entities;
using Craftsman.xCoronavirus.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Linq;

namespace Craftsman.xCoronavirus.Infrastructure
{
    public class ModifyRequestRepository: IModifyRequestRepository
    {
        private IRepository<ModifyRequest, string> _repoModifyRequest;
        private IRepository<ModifyRequestMaterialDemand, string> _repoMaterialDemand;
        private IRepository<ModifyRequestContact, string> _repoContact;
        private IRepository<Hospital, string> _repoHospital;

        public ModifyRequestRepository(
            IRepository<ModifyRequest, string> repoModifyRequest,
            IRepository<ModifyRequestMaterialDemand, string> repoMaterialDemand,
            IRepository<ModifyRequestContact, string> repoContact,
            IRepository<Hospital, string> repoHospital
        )
        {
            _repoModifyRequest = repoModifyRequest;
            _repoMaterialDemand = repoMaterialDemand;
            _repoContact = repoContact;
            _repoHospital = repoHospital;
        }

        public IQueryable<ModifyRequest> GetAll()
        {
            return _repoModifyRequest.GetAll();
        }

        public bool ExistModifyRequest(Expression<Func<ModifyRequest, bool>> predicate)
        {
            return !(_repoModifyRequest.FirstOrDefault(predicate) == null);
        }

        public ModifyRequest GetModifyRequest(string guid)
        {
            var modifyRequest = _repoModifyRequest.FirstOrDefault(x => x.Id == guid);
            if (modifyRequest == null) return null;

            modifyRequest.MaterialDemands = _repoMaterialDemand.GetAllList(x => x.ModifyRequestId == guid);
            modifyRequest.Contacts = _repoContact.GetAllList(x => x.ModifyRequestId == guid);
            return modifyRequest;
        }

        public List<ModifyRequest> GetAllModifyRequests(bool withDetails = false)
        {

            var modifyRequests = _repoModifyRequest.GetAllList();
            if (modifyRequests == null) return null;

            if (withDetails)
            {
                foreach (var modifyRequest in modifyRequests)
                {
                    modifyRequest.MaterialDemands = _repoMaterialDemand.GetAllList(x => x.ModifyRequestId == modifyRequest.Id);
                    modifyRequest.Contacts = _repoContact.GetAllList(x => x.ModifyRequestId == modifyRequest.Id);
                }
            }
            return modifyRequests;
        }

        public ModifyRequest UpdateModifyRequest(ModifyRequest entity)
        {
            _repoModifyRequest.Update(entity);
            if (entity.MaterialDemands != null && entity.MaterialDemands.Count > 0)
            {
                foreach (var materialDemand in entity.MaterialDemands)
                {
                    _repoMaterialDemand.InsertOrUpdate(materialDemand);
                }
            }

            if (entity.Contacts != null && entity.Contacts.Count > 0)
            {
                foreach (var contact in entity.Contacts)
                {
                    _repoContact.InsertOrUpdate(contact);
                }
            }
            return entity;
        }

        public ModifyRequest CreatModifyRequest(ModifyRequest entity)
        {
            _repoModifyRequest.Insert(entity);
            if (entity.MaterialDemands != null && entity.MaterialDemands.Count > 0)
            {
                foreach (var materialDemand in entity.MaterialDemands)
                {
                    _repoMaterialDemand.Insert(materialDemand);
                }
            }

            if (entity.Contacts != null && entity.Contacts.Count > 0)
            {
                foreach (var contact in entity.Contacts)
                {
                    _repoContact.Insert(contact);
                }
            }
            return entity;
        }
    }
}
